#include "lvgl.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* 游戏常量定义 */
#define SCREEN_WIDTH      1024     // 屏幕宽度
#define SCREEN_HEIGHT     600      // 屏幕高度
#define PLANE_WIDTH       60       // 飞机宽度
#define PLANE_HEIGHT      60       // 飞机高度
#define BULLET_WIDTH      6        // 子弹宽度
#define BULLET_HEIGHT     14       // 子弹高度
#define ENEMY_WIDTH       50       // 敌机宽度
#define ENEMY_HEIGHT      50       // 敌机高度
#define BULLET_SPEED      10       // 子弹速度
#define ENEMY_SPEED       3        // 敌机速度
#define MAX_BULLETS       20       // 最大子弹数量
#define MAX_ENEMIES       10       // 最大敌机数量

/* 游戏对象结构体 */
typedef struct {
    lv_obj_t *obj;
    int x;
    int y;
    bool active;
} Bullet;

typedef struct {
    lv_obj_t *obj;
    int x;
    int y;
    bool active;
} Enemy;

/* 游戏全局变量 */
static lv_obj_t *player_plane;
static int player_x = SCREEN_WIDTH / 2 - PLANE_WIDTH / 2;
static int player_y = SCREEN_HEIGHT - PLANE_HEIGHT - 20;
static Bullet bullets[MAX_BULLETS];
static Enemy enemies[MAX_ENEMIES];
static int score = 0;
static lv_obj_t *score_label;
static bool game_running = true;
static lv_obj_t *game_over_label;

/* 初始化子弹 */
static void init_bullets(void) {
    for (int i = 0; i < MAX_BULLETS; i++) {
        bullets[i].obj = lv_obj_create(lv_scr_act());
        lv_obj_set_size(bullets[i].obj, BULLET_WIDTH, BULLET_HEIGHT);
        lv_obj_set_style_bg_color(bullets[i].obj, lv_color_hex(0xFFFF00), 0); // 黄色子弹
        lv_obj_set_style_radius(bullets[i].obj, 3, 0);
        bullets[i].active = false;
        lv_obj_add_flag(bullets[i].obj, LV_OBJ_FLAG_HIDDEN);
    }
}

/* 初始化敌机 */
static void init_enemies(void) {
    srand(time(NULL));
    for (int i = 0; i < MAX_ENEMIES; i++) {
        enemies[i].obj = lv_img_create(lv_scr_act());
        lv_img_set_src(enemies[i].obj, "A:/XXX.BMP");
        lv_obj_set_size(enemies[i].obj, ENEMY_WIDTH, ENEMY_HEIGHT);
        lv_obj_add_flag(enemies[i].obj, LV_OBJ_FLAG_CLICKABLE); // 允许触摸检测
        enemies[i].active = false;
        lv_obj_add_flag(enemies[i].obj, LV_OBJ_FLAG_HIDDEN);
    }
}

/* 发射子弹 */
static void fire_bullet(void) {
    for (int i = 0; i < MAX_BULLETS; i++) {
        if (!bullets[i].active) {
            bullets[i].x = player_x + PLANE_WIDTH / 2 - BULLET_WIDTH / 2;
            bullets[i].y = player_y;
            lv_obj_set_pos(bullets[i].obj, bullets[i].x, bullets[i].y);
            bullets[i].active = true;
            lv_obj_clear_flag(bullets[i].obj, LV_OBJ_FLAG_HIDDEN);
            break;
        }
    }
}

/* 生成敌机 */
static void spawn_enemy(void) {
    for (int i = 0; i < MAX_ENEMIES; i++) {
        if (!enemies[i].active) {
            // 随机x坐标，确保敌机在屏幕内
            enemies[i].x = rand() % (SCREEN_WIDTH - ENEMY_WIDTH);
            enemies[i].y = -ENEMY_HEIGHT; // 从屏幕顶部外出现
            lv_obj_set_pos(enemies[i].obj, enemies[i].x, enemies[i].y);
            enemies[i].active = true;
            lv_obj_clear_flag(enemies[i].obj, LV_OBJ_FLAG_HIDDEN);
            break;
        }
    }
}

/* 检查碰撞 */
static bool check_collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) {
    return (x1 < x2 + w2 &&
            x1 + w1 > x2 &&
            y1 < y2 + h2 &&
            y1 + h1 > y2);
}

/* 更新游戏状态 */
static void update_game(lv_timer_t *timer) {
    if (!game_running) return;
    
    // 更新子弹位置
    for (int i = 0; i < MAX_BULLETS; i++) {
        if (bullets[i].active) {
            bullets[i].y -= BULLET_SPEED;
            lv_obj_set_y(bullets[i].obj, bullets[i].y);
            
            // 子弹超出屏幕范围
            if (bullets[i].y < -BULLET_HEIGHT) {
                bullets[i].active = false;
                lv_obj_add_flag(bullets[i].obj, LV_OBJ_FLAG_HIDDEN);
            }
        }
    }
    
    // 随机生成敌机
    if (rand() % 100 < 5) { // 5%的概率生成敌机
        spawn_enemy();
    }
    
    // 更新敌机位置
    for (int i = 0; i < MAX_ENEMIES; i++) {
        if (enemies[i].active) {
            enemies[i].y += ENEMY_SPEED;
            lv_obj_set_y(enemies[i].obj, enemies[i].y);
            
            // 敌机超出屏幕范围
            if (enemies[i].y > SCREEN_HEIGHT) {
                enemies[i].active = false;
                lv_obj_add_flag(enemies[i].obj, LV_OBJ_FLAG_HIDDEN);
            }
        }
    }
    
    // 碰撞检测 - 子弹击中敌机
    for (int i = 0; i < MAX_BULLETS; i++) {
        if (bullets[i].active) {
            for (int j = 0; j < MAX_ENEMIES; j++) {
                if (enemies[j].active) {
                    if (check_collision(bullets[i].x, bullets[i].y, BULLET_WIDTH, BULLET_HEIGHT,
                                       enemies[j].x, enemies[j].y, ENEMY_WIDTH, ENEMY_HEIGHT)) {
                        // 击中敌机，销毁子弹和敌机
                        bullets[i].active = false;
                        lv_obj_add_flag(bullets[i].obj, LV_OBJ_FLAG_HIDDEN);
                        
                        enemies[j].active = false;
                        lv_obj_add_flag(enemies[j].obj, LV_OBJ_FLAG_HIDDEN);
                    }
                }
            }
        }
    }
    
    // 碰撞检测 - 敌机撞玩家
    for (int i = 0; i < MAX_ENEMIES; i++) {
        if (enemies[i].active) {
            // 调整碰撞区域，适配图片透明部分
            if (check_collision(
                player_x + 5, player_y + 5, 
                PLANE_WIDTH - 10, PLANE_HEIGHT - 10,
                enemies[i].x + 5, enemies[i].y + 5, 
                ENEMY_WIDTH - 10, ENEMY_HEIGHT - 10
            )) {
                // 游戏结束
                game_running = false;
                lv_label_set_text(game_over_label, "Game Over!\nTap to restart");
                lv_obj_clear_flag(game_over_label, LV_OBJ_FLAG_HIDDEN);
            }
        }
    }
}

/* 飞机触摸事件处理 - 负责飞机移动 */
static void plane_touch_handler(lv_event_t *e) {
    lv_event_code_t code = lv_event_get_code(e);
    lv_indev_t *indev = lv_indev_get_act();

    if (!game_running) return;

    static lv_point_t last_point;
    static bool is_first_press = false;

    if (code == LV_EVENT_PRESSING) {
        lv_point_t curr_point;
        lv_indev_get_point(indev, &curr_point);

        if (!is_first_press) {
            // 首次按压：记录初始位置
            last_point = curr_point;
            is_first_press = true;
        } else {
            // 按压移动中：计算位移并更新飞机位置
            int dx = curr_point.x - last_point.x;
            int dy = curr_point.y - last_point.y;

            // 更新玩家位置
            player_x += dx;
            player_y += dy;

            // 限制飞机在屏幕内
            if (player_x < 0) player_x = 0;
            if (player_x > SCREEN_WIDTH - PLANE_WIDTH) 
                player_x = SCREEN_WIDTH - PLANE_WIDTH;
            if (player_y < 0) player_y = 0;
            if (player_y > SCREEN_HEIGHT - PLANE_HEIGHT) 
                player_y = SCREEN_HEIGHT - PLANE_HEIGHT;

            // 刷新飞机位置
            lv_obj_set_pos(player_plane, player_x, player_y);

            // 更新上一次位置
            last_point = curr_point;
        }
    } else if (code == LV_EVENT_RELEASED) {
        // 触摸释放：重置标记
        is_first_press = false;
    }
}

/* 屏幕点击事件处理 - 负责发射子弹和游戏重启 */
static void screen_click_handler(lv_event_t *e) {
    lv_event_code_t code = lv_event_get_code(e);

    if (!game_running) {
        // 游戏结束时点击屏幕重启游戏
        if (code == LV_EVENT_CLICKED) {
            // 重置游戏状态
            game_running = true;
            score = 0;
            lv_label_set_text(score_label, "Score: 0");
            lv_obj_add_flag(game_over_label, LV_OBJ_FLAG_HIDDEN);
            
            // 重置玩家位置
            player_x = SCREEN_WIDTH / 2 - PLANE_WIDTH / 2;
            player_y = SCREEN_HEIGHT - PLANE_HEIGHT - 20;
            lv_obj_set_pos(player_plane, player_x, player_y);
            
            // 隐藏所有子弹和敌机
            for (int i = 0; i < MAX_BULLETS; i++) {
                bullets[i].active = false;
                lv_obj_add_flag(bullets[i].obj, LV_OBJ_FLAG_HIDDEN);
            }
            for (int i = 0; i < MAX_ENEMIES; i++) {
                enemies[i].active = false;
                lv_obj_add_flag(enemies[i].obj, LV_OBJ_FLAG_HIDDEN);
            }
        }
        return;
    }
    
    // 游戏运行中点击屏幕发射子弹
    if (code == LV_EVENT_CLICKED) {
        fire_bullet();
    }
}

/* 自动发射子弹的定时器回调 */
static void auto_fire_timer(lv_timer_t *timer) {
    if (game_running) {
        fire_bullet();
    }
}

/* 初始化游戏 */
void plane_war_init(void) {
    // 设置屏幕背景
    lv_obj_set_style_bg_color(lv_scr_act(), lv_color_hex(0x000000), 0); // 黑色背景
    
    // 创建玩家飞机
    player_plane = lv_img_create(lv_scr_act());
    lv_img_set_src(player_plane, "A:/XXX.BMP");
    lv_obj_set_size(player_plane, PLANE_WIDTH, PLANE_HEIGHT); // 设置飞机尺寸
    player_x = SCREEN_WIDTH / 2 - PLANE_WIDTH / 2;
    player_y = SCREEN_HEIGHT - PLANE_HEIGHT - 20;
    lv_obj_set_pos(player_plane, player_x, player_y);
    lv_obj_add_flag(player_plane, LV_OBJ_FLAG_CLICKABLE); // 允许触摸事件
    
    // 创建游戏结束标签
    game_over_label = lv_label_create(lv_scr_act());
    lv_obj_set_style_text_color(game_over_label, lv_color_hex(0xFFFFFF), 0);
    lv_obj_set_style_text_font(game_over_label, &lv_font_montserrat_32, 0);
    lv_obj_align(game_over_label, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_flag(game_over_label, LV_OBJ_FLAG_HIDDEN);
    
    // 初始化子弹和敌机
    init_bullets();
    init_enemies();
    
    // 绑定事件：飞机负责移动，屏幕负责点击发射和重启
    lv_obj_add_event_cb(player_plane, plane_touch_handler, LV_EVENT_ALL, NULL);
    lv_obj_add_event_cb(lv_scr_act(), screen_click_handler, LV_EVENT_ALL, NULL);
    
    // 创建游戏更新定时器 (30ms更新一次，约33FPS)
    lv_timer_create(update_game, 30, NULL);
    
    // 创建自动发射子弹定时器 (500ms发射一次)
    lv_timer_create(auto_fire_timer, 500, NULL);
}

//在main函数中调用plane_war_init()即可